/*
 * Created by JFormDesigner on Thu Oct 28 10:28:47 CST 2021
 */

package view.CURD;

import library.controller.FrameListener;
import library.po.Book;
import library.po.Borrow;
import library.po.User;
import library.service.UserService;
import library.service.impl.UserServiceImpl;
import view.main.MainFrame;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author unknown
 */
public class BorrowDialog extends JDialog {
    public BorrowDialog(BookViewDialog owner, String title, Book book, User user) {
        super(owner);
        this.setTitle(title);
        this.book = book;
        this.user = userService.selectById(user.getUserId());
        this.setVisible(true);
        this.listener = owner.getListener();
        initComponents();
    }
    private void initComponents() {
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        userIdLabel = new JLabel();
        ISBNLabel = new JLabel();
        bookNameLabel = new JLabel();
        idValue = new JTextField();
        ISBNValue = new JTextField();
        bookNameValue = new JTextField();
        okButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax .
            swing. border .EmptyBorder ( 0, 0 ,0 , 0) ,  "" , javax. swing .border
            . TitledBorder. CENTER ,javax . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog"
            , java .awt . Font. BOLD ,12 ) ,java . awt. Color .red ) ,dialogPane. getBorder
            () ) ); dialogPane. addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java
            . beans. PropertyChangeEvent e) { if( "\u0062ord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException
            ( ) ;} } );
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- userIdLabel ----
                userIdLabel.setText("\u501f\u9605\u4eba\u7528\u6237\u540d\uff1a");
                userIdLabel.setFont(new Font("\u5e7c\u5706", userIdLabel.getFont().getStyle() & ~Font.BOLD, userIdLabel.getFont().getSize() + 4));
                contentPanel.add(userIdLabel);
                userIdLabel.setBounds(new Rectangle(new Point(10, 75), userIdLabel.getPreferredSize()));

                //---- ISBNLabel ----
                ISBNLabel.setText("\u501f\u9605\u4e66\u7c4dISBN\uff1a");
                ISBNLabel.setFont(new Font("\u5e7c\u5706", ISBNLabel.getFont().getStyle() & ~Font.BOLD, ISBNLabel.getFont().getSize() + 4));
                contentPanel.add(ISBNLabel);
                ISBNLabel.setBounds(new Rectangle(new Point(10, 155), ISBNLabel.getPreferredSize()));

                //---- bookNameLabel ----
                bookNameLabel.setText("\u501f\u9605\u4e66\u7c4d\u4e66\u540d\uff1a");
                bookNameLabel.setFont(new Font("\u5e7c\u5706", bookNameLabel.getFont().getStyle() & ~Font.BOLD, bookNameLabel.getFont().getSize() + 4));
                contentPanel.add(bookNameLabel);
                bookNameLabel.setBounds(new Rectangle(new Point(10, 235), bookNameLabel.getPreferredSize()));
                contentPanel.add(idValue);
                idValue.setFont(new Font("\u5e7c\u5706", bookNameLabel.getFont().getStyle() & ~Font.BOLD, bookNameLabel.getFont().getSize() + 4));
                idValue.setBounds(125, 75, 140, idValue.getPreferredSize().height);
                contentPanel.add(ISBNValue);
                ISBNValue.setFont(new Font("\u5e7c\u5706", bookNameLabel.getFont().getStyle() & ~Font.BOLD, bookNameLabel.getFont().getSize() + 4));
                ISBNValue.setBounds(125, 155, 140, ISBNValue.getPreferredSize().height);
                contentPanel.add(bookNameValue);
                bookNameValue.setFont(new Font("\u5e7c\u5706", bookNameLabel.getFont().getStyle() & ~Font.BOLD, bookNameLabel.getFont().getSize() + 4));
                bookNameValue.setBounds(125, 235, 140, bookNameValue.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //---- okButton ----
            okButton.setText("\u786e\u8ba4");
            okButton.addActionListener(listener);
            okButton.setActionCommand("InsertBorrow");
            okButton.setFont(okButton.getFont().deriveFont(okButton.getFont().getSize() + 4f));
            dialogPane.add(okButton, BorderLayout.SOUTH);
        }

        if ("借阅卡".equals(this.getTitle())){
            contentPanel.remove(bookNameValue);
            bookNameValue.setText(book.getBookName());
            bookNameValue.setEditable(false);
            contentPane.add(bookNameValue);
            contentPane.remove(idValue);
            idValue.setText(user.getUserId());
            idValue.setEditable(false);
            contentPane.add(idValue);
            contentPane.remove(ISBNValue);
            ISBNValue.setText(book.getBookIsbn().toString());
            ISBNValue.setEditable(false);
            contentPane.add(ISBNValue);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(335, 415);
        setLocationRelativeTo(getOwner());
    }
    public Book getBook(){
        return this.book;
    }
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel userIdLabel;
    private JLabel ISBNLabel;
    private JLabel bookNameLabel;
    private JTextField idValue;
    private JTextField ISBNValue;
    private JTextField bookNameValue;
    private JButton okButton;
    private Borrow borrow;
    private User user;
    private Book book;
    private FrameListener listener;
    private UserService userService = new UserServiceImpl();
}
